<template>
  <div class="weight_control">
    <info-card :title="$t('weight_control')">
      <div class="table_info">
        <div class="row" v-for="item in tableDate" :key="item.name + item.value">
          <div class="cell item_name">{{ item.name }}</div>
          <div class="cell">
            {{ item.value }}<span v-if="weight_unit !== 'st:lb'">{{ item.unit }}</span>
          </div>
        </div>
      </div>
    </info-card>
  </div>
</template>

<script>
import { toFixedNumber } from '@/assets/ts/eight_electrodes_report/Util'
import InfoCard from './InfoCard.vue'

export default {
  props: {
    measureData: {
      type: Object,
      required: true,
    },
    weight_unit: {
      type: String,
      required: true,
    },
  },
  components: {
    InfoCard,
  },
  data() {
    return {
      tableDate: [],
    }
  },
  mounted() {
    const measureData = this.measureData
    const ReportDetail = new EightReport.default(this.measureData)
    const ReportIdsMapping = EightReport.ReportIdsMappings
    const pickReports = ReportDetail.pickReports([
      ReportIdsMapping.Bone,
      ReportIdsMapping.Water,
      ReportIdsMapping.Protein,
    ])
    /**
     * 标准的去脂体重计算方式    FFM
     * 体水分，蛋白质，无机盐标准范围中值相加，也就是范围最大值加最小值的平均值
     *
     * 实测去脂体重计算方式 reallyFFM
     * 体水分，蛋白质，无机盐值相加
     *
     * 肌肉控制计算过程  muscleControl
     * 1.求标准的去脂体重（FFM）：范围中值
     * 2.测得的去脂体重（FFM） - 范围中值 > 0?
     * 则 肌肉控制 = 0；（肌肉超过标准值不需要控制）
     * 否则 肌肉控制 = 范围中值 - 测得的去脂体重；
     */
    let FFM = 0 // 标准的去脂体重
    let reallyFFM = 0
    let muscleControl = 0
    pickReports.map((item) => {
      FFM += EightReport.util.boundariesAverage(item.boundaries)
      reallyFFM += toFixedNumber(item.value, 2)
    })
    if (FFM > reallyFFM) muscleControl = FFM - reallyFFM

    /**
     * 脂肪控制计算过程 fatControl
     *
     * 男：体脂正常范围10% ~ 20% 取中值 15%
     * 女：体脂正常范围18% ~ 28% 取中值 23%
     * 脂肪控制 = 体重 x （15% or 23 %） - 测量脂肪量
     * 脂肪控制 = (标准体脂率*体重 - 测量脂肪量) / (1-标准体脂率)
     */
    const bodyFatRange = measureData.gender === 1 ? 0.15 : 0.23
    // let fatControl = measureData.weight * (bodyFatRange - measureData.bodyfat / 100);
    let fatControl = 0
    if (muscleControl === 0) {
      fatControl =
        (bodyFatRange * measureData.weight - measureData.bodyfat * measureData.weight * 0.01) /
        (1 - bodyFatRange)
    } else {
      fatControl = (bodyFatRange*(measureData.weight + muscleControl)-measureData.bodyfat* measureData.weight * 0.01)/(1-bodyFatRange)
    }
    /**
     * 体重控制计算过程 weightControl
     * 体重控制为： 脂肪控制值 + 肌肉控制值
     */
    const weightControl = fatControl + muscleControl
    /**
     * 标准体重计算过程 weightStandard
     * 标准体重 = 测量体重 + 体重控制值
     */
    const weightStandard = measureData.weight + weightControl
    /**
     * 赋值
     */
    if (this.weight_unit !== 'st:lb') {
      this.tableDate = [
        {
          name: this.$t('target_weight'),
          value: this.unitCovert(weightStandard).toFixed(2),
          unit: this.weight_unit,
        },
        {
          name: this.$t('weight_control'),
          value: this.handleValueAddPlus(this.unitCovert(weightControl)),
          unit: this.weight_unit,
        },
        {
          name: this.$t('fat_control'),
          value: this.handleValueAddPlus(this.unitCovert(fatControl)),
          unit: this.weight_unit,
        },
        {
          name: this.$t('muscle_control'),
          value: this.handleValueAddPlus(this.unitCovert(muscleControl)),
          unit: this.weight_unit,
        },
      ]
    } else {
      this.tableDate = [
        {
          name: this.$t('target_weight'),
          value: this.handleValueAddPlus(this.unitCovert(weightStandard), false),
          unit: this.weight_unit,
        },
        {
          name: this.$t('weight_control'),
          value: this.handleValueAddPlus(this.unitCovert(weightControl)),
          unit: this.weight_unit,
        },
        {
          name: this.$t('fat_control'),
          value: this.handleValueAddPlus(this.unitCovert(fatControl)),
          unit: this.weight_unit,
        },
        {
          name: this.$t('muscle_control'),
          value: this.handleValueAddPlus(this.unitCovert(muscleControl)),
          unit: this.weight_unit,
        },
      ]
    }
  },
  methods: {
    handleValueAddPlus(value, symbol = true) {
      if (this.weight_unit !== 'st:lb') {
        value = EightReport.util.toFixedNumber(value, 2)
        let returnValue = value
        if (value > 0) returnValue = `+${value}`
        return returnValue
      } else {
        let str = `${value[0]}st${value[1]}lb`
        if (symbol && value[0] > 0) {
          str = `+${value[0]}st${value[1]}lb`
        }
        return str
      }
    },
  },
}
</script>

<style lang="scss" scoped>
@import '../index.scss';
.table_info {
  border-top: 1px solid $mainColor31;
  border-bottom: 1px solid $mainColor31;
  // margin-bottom: 17px;
}
.row {
  display: flex;
}
.row:not(:last-child) {
  border-bottom: 1px solid $mainColor31;
}
.cell {
  width: 155px;
  height: 34px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 13px;
  font-weight: 600;
}
.cell:not(:nth-child(2n + 2)) {
  border-right: 1px solid $mainColor31;
}
.item_name {
  font-weight: 600;
  letter-spacing: -1px;
}
</style>
